package com.library.system.modules.examine.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.library.common.enums.AuditStatusEnum;
import com.library.common.enums.ClassifyEnum;
import com.library.system.modules.examine.entity.Examine;
import com.library.system.modules.examine.service.BorrowingAuditService;
import com.library.system.modules.examine.vo.ExamineVO;
import com.library.system.modules.examine.bo.ExaminePage;
import com.library.system.modules.examine.bo.ExamineInsert;
import com.library.system.modules.examine.bo.ExamineUpdate;
import com.library.system.modules.examine.mapper.ExamineMapper;
import com.library.system.modules.examine.struct.ExamineStructMapper;
import com.library.system.modules.examine.service.ExamineService;
import com.library.system.modules.notice.service.NoticeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import com.library.common.util.PageCovertUtil;
import org.springframework.transaction.annotation.Transactional;


/**
 * @Author xyh
 * @Desc 审核表(Examine)表服务实现类
 * @Date 2023-10-05 16:35:23
 */

@Log4j2
@Service("examineService")
public class ExamineServiceImpl extends ServiceImpl<ExamineMapper, Examine> implements ExamineService {

    @Resource
    private ExamineStructMapper examineStructMapper;
    @Resource
    private NoticeService noticeService;
    @Resource
    private BorrowingAuditService borrowingAuditService;


    @Override
    public IPage<ExamineVO> queryByPage(ExaminePage page) {
        // 查询条件
        LambdaQueryWrapper<Examine> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(page.getTitle())) {
            queryWrapper.like(Examine::getTitle, page.getTitle());
        }
        if (StrUtil.isNotEmpty(page.getSubmitUsername())) {
            queryWrapper.like(Examine::getSubmitUsername, page.getSubmitUsername());
        }
        if (page.getClassify() != null) {
            queryWrapper.eq(Examine::getClassify, page.getClassify());
        } else {
            queryWrapper.in(Examine::getClassify, ClassifyEnum.NOTICE.getCode(), ClassifyEnum.SYSTEM_NOTICE.getCode());
        }
        queryWrapper.orderByDesc(Examine::getCreateTime);
        // 查询分页数据
        Page<Examine> examinePage = new Page<Examine>(page.getCurrent(), page.getSize());
        IPage<Examine> pageData = baseMapper.selectPage(examinePage, queryWrapper);

        //转换成vo
        IPage<ExamineVO> records = PageCovertUtil.pageVoCovert(pageData, ExamineVO.class);
        if (CollUtil.isNotEmpty(records.getRecords())) {
            records.getRecords().forEach(r -> {
                r.setClassifyName(ClassifyEnum.getValue(r.getClassify()));
            });
        }
        return records;
    }


    @Override
    public ExamineVO queryById(Integer id) {
        Examine examine = baseMapper.selectById(id);
        ExamineVO examineVO = examineStructMapper.examineToExamineVO(examine);
        if (examineVO != null) {
            examineVO.setClassifyName(ClassifyEnum.getValue(examineVO.getClassify()));
        }
        return examineVO;
    }

    @Override
    public boolean insertExamine(ExamineInsert examineInsert, ClassifyEnum classifyEnum) {
        Examine examine = examineStructMapper.insertToExamine(examineInsert);
        examine.setExamineStatus(AuditStatusEnum.REVIEWING.getCode());
        examine.setClassify(classifyEnum.getCode());
        save(examine);
        return true;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean examineUpdate(ExamineUpdate examineUpdate) {
        Examine examine = examineStructMapper.updateToExamine(examineUpdate);
        updateById(examine);
        //修改公告审核通过的状态
        ExamineVO examineVO = queryById(examineUpdate.getId());
        switch (examineVO.getClassify()) {
            case 0:
            case 1:
                if (AuditStatusEnum.AUDIT_SUCCESS.getCode().equals(examineVO.getExamineStatus())) {
                    noticeService.updateNoticeStatus(examineVO.getClassifyId(), AuditStatusEnum.AUDIT_SUCCESS, examineVO.getAdvice());
                    log.info("通知公告审核通过，公告id为：{}", examineVO.getClassifyId());
                } else {
                    noticeService.updateNoticeStatus(examineVO.getClassifyId(), AuditStatusEnum.REJECT, examineVO.getAdvice());
                    log.info("通知公告审核不通过，公告id为：{}", examineVO.getClassifyId());
                }
                break;
            case 2:
                if (AuditStatusEnum.AUDIT_SUCCESS.getCode().equals(examineVO.getExamineStatus())) {
                    borrowingAuditService.updateBorrowingStatus(examineVO.getClassifyId(), AuditStatusEnum.AUDIT_SUCCESS, examineVO.getAdvice());
                    log.info("图书归还审核通过，提交审核人为：{}", examineVO.getSubmitUsername());
                } else {
                    borrowingAuditService.updateBorrowingStatus(examineVO.getClassifyId(), AuditStatusEnum.REJECT, examineVO.getAdvice());
                    log.info("通知公告审核不通过，提交审核人为：{}", examineVO.getSubmitUsername());
                }
                break;
            default:
                return false;
        }
        return true;
    }

    @Override
    public boolean deleteById(Integer id) {
        return removeById(id);
    }

}
